package org.aman.publish.util;

import com.jcraft.jsch.*;

import java.util.Properties;

/**
 * @author DaiJG
 * @title SshUtil功能描述
 * @Date created at 2019/8/30 12:01
 * @Description
 * @since 1.0.0
 */
public class SshUtil {
    
    public static Session getSession(String host, int port, String username, final String password) {
        Session session = null;
        try {
            JSch jsch = new JSch();
            session = jsch.getSession(username, host, port);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            //session.setUserInfo(new MyUserInfo());
            session.connect();
        } catch (JSchException e) {
            e.printStackTrace();
        }
        return session;
    }
    
    public static void close(Channel channel, Session session){
        if(channel != null){
            try {
                channel.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if(session != null){
            try {
                session.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
